package z_multichat;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileUpLoadServerThread extends Thread {
	int port;
	ServerSocket serverSocket;
	Socket socket;
	String oriFileName;
	String folder;
	byte[] data = new byte[1024];

	public FileUpLoadServerThread(int port, String folder, String oriFileName) {
		this.port = port;
		this.folder = folder;
		this.oriFileName = oriFileName;
	}

	public void run() {
		try {
			serverSocket = new ServerSocket(port);
			socket = serverSocket.accept();
			
			FileInputStream fis = new FileInputStream((folder + oriFileName));
			DataOutputStream out = new DataOutputStream(
					socket.getOutputStream());
			
			out.writeUTF(oriFileName);

			int len = 0;
			long tempLen = 0l;
			while ((len = fis.read(data, 0, data.length)) != -1) {
				out.write(data, 0, len);
				tempLen += len;
				System.out.println("up load size : " + len + ",tot="
						+ tempLen);
			}
			out.flush();
			out.close();
			fis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
